package com.briup.srms.sms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.srms.sms.bean.Department;
import com.briup.srms.sms.bean.extend.DepartmentExtend;
import com.briup.srms.sms.dao.DepartmentMapper;
import com.briup.srms.sms.dao.extend.DepartmentExtendMapper;
import com.briup.srms.sms.service.IDepartmentService;
import com.jacky.base.utils.CustomerException;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
	@Autowired
	private DepartmentMapper departmentMapper;
	@Autowired
	private DepartmentExtendMapper departmentExtendMapper;

	@Override
	public List<DepartmentExtend> queryTree() {
		return departmentExtendMapper.queryTree();
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		Department department = departmentMapper.selectByPrimaryKey(id);

		if(id<=10) {
			throw new CustomerException("内置部门不可删除");
		}
		if(department == null) {
			throw new CustomerException("要删除部门信息不存在");
		}
		departmentMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void saveOrUpdate(Department department) throws CustomerException {
		if(department.getId() == null) {
			departmentMapper.insert(department);
		} else {
			departmentMapper.updateByPrimaryKey(department);
		}
	}

}
